package com.jacky.javafx.fx.controller;

import com.jacky.javafx.AppUserRepository;
import com.jacky.javafx.entities.AppUserEntity;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.xml.transform.Source;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.ResourceBundle;

@Component
public class MainController implements Initializable {

    private Long selectedId;
    private AppUserEntity selectedAppUser;

    @Autowired
    private AppUserRepository repository;
    @FXML
    private AnchorPane anchorPane;
    @FXML
    private ListView<AppUserEntity> list;

    @FXML
    private TextField txtId, txtName, txtTel;
    @FXML
    private Button btnCreate, btnClear, btnUpdate, btnDelete;
    @FXML
    private Label lblTotal, lblWarning;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        txtId.setDisable(true);
        list(); // 加载数据并展示到listView
        list.getSelectionModel().selectedItemProperty()
                .addListener((obs, oldVal, newVal) -> {
                    if (newVal != null) {
                        System.out.println("selected id:" + newVal.getId());
                        selectedId = newVal.getId();
                        selectedAppUser = getItemById(newVal.getId());
                        txtId.setText(String.valueOf(selectedAppUser.getId()));
                        txtName.setText(String.valueOf(selectedAppUser.getLoginName()));
                        txtTel.setText(String.valueOf(selectedAppUser.getMobile()));


                        btnClear.setVisible(true);
                        btnCreate.setVisible(false);
                        btnDelete.setVisible(true);
                        btnUpdate.setVisible(true);

                        lblWarning.setVisible(false);
                    }
                });
    }

    private AppUserEntity getItemById(Long id) {
        return repository.findById(id).get();
    }

    public void list() {
        list.setItems(FXCollections.observableList(repository.findAll()));
        lblTotal.setText(String.valueOf(list.getItems().size()));
    }

    @FXML
    public void add() {
        if (!txtName.getText().trim().isEmpty()) {
            AppUserEntity newUser = new AppUserEntity();
            newUser.setLoginName(txtName.getText().trim());
            newUser.setMobile(txtTel.getText().trim());
            newUser.setPassword("123456");
            newUser.setRegistryTime(LocalDateTime.now());
            newUser.setState("NORMAL");
            AppUserEntity save = repository.save(newUser);
            System.out.println(save);
            list();
            clear();
        } else {
            lblWarning.setVisible(true);
        }
    }

    @FXML
    public void clear() {
//        txtId.clear();
//        txtName.clear();
//        txtTel.clear();
        anchorPane.getChildren().forEach(node -> {
            if (node instanceof TextField)
                ((TextField) node).clear();
        });

//        txtId.setDisable(false);
        btnClear.setVisible(false);
        btnCreate.setVisible(true);
        btnDelete.setVisible(false);
        btnUpdate.setVisible(false);

        lblWarning.setVisible(true);
    }

    @FXML
    public void update() {
        selectedAppUser.setMobile(txtTel.getText());
        selectedAppUser.setLoginName(txtName.getText());
        repository.save(selectedAppUser);
        list();
    }

    @FXML
    public void delete() {
        repository.deleteById(selectedId);
        clear();
        list();
        txtId.setDisable(false);
        btnClear.setVisible(false);
        btnCreate.setVisible(true);
        btnDelete.setVisible(false);
        btnUpdate.setVisible(false);

    }
}
